home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / udos / fgrep103 / getpatfi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-29  |  1.7 KB  |  60 lines

  1. #include <stdio.h>
  2. #include <types.h>
  3. #include <stat.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include <alloc.h> /* N2 04-05-91 */
  7. #include "bm.h"
  8. #include "proto.h" /* N2 04-05-91 */
  9.  
  10. int GetPatFile(char *PatFile,struct PattDesc *DescVec[])
  11. /* --------------
  12.     char *PatFile;
  13.     struct PattDesc *DescVec[];
  14. ----------------- */
  15.     /* read patterns from a file and set up a pattern descriptor vector */
  16.     {
  17.         /* extern char *malloc(); N2 04-05-91 */
  18.         int PFile;
  19.         struct stat StatBuff;
  20.     int PatSize;       /* the number of chars in all the patterns */
  21.     char *PatBuff = NULL;     /* hold the patterns */
  22.     int file_size;
  23.  
  24.         if ((PFile = open(PatFile,O_TEXT)) < 0)
  25.         {
  26.           fprintf(stderr,"bm: can't open pattern file %s\n",PatFile);
  27.           exit(2);
  28.         } /* if */
  29.         /* find out how big the patterns are */
  30.         if (fstat(PFile,&StatBuff) == -1)
  31.         {
  32.           fprintf(stderr,"bm: can't fstat %s\n",PatFile);
  33.           exit(2);
  34.         } /* if */
  35.     PatSize = (int)StatBuff.st_size;
  36.     if (!PatSize)
  37.     {
  38.       fprintf(stderr,"bm: pattern file is empty\n");
  39.       exit(2);
  40.     } /* if */
  41.     if (!(PatBuff = (char*)malloc(PatSize))) // 11-28-91
  42.     {
  43.       fprintf(stderr,"bm: insufficient memory to store patterns\n");
  44.           exit(2);
  45.         } /* if */
  46.         file_size = read(PFile,PatBuff,PatSize); /* get the patterns */
  47.         close(PFile);
  48.         /* make sure the patterns are null-terminated. We can't have
  49.         * nulls in the patterns */
  50.         if (PatBuff[file_size-1] == '\n')
  51.          {
  52.           PatBuff[file_size-1] = NULL;
  53.          }
  54.         else
  55.          {
  56.           PatBuff[file_size] = NULL;
  57.          }
  58.         return(MkDescVec(DescVec,PatBuff));
  59.      } /* GetPatFile */
  60.